﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Xml.Serialization;
using System.Resources;
using System.Reflection;

namespace SPFarmModeler.Common
{
    public class Utilities
    {
        /// <summary>
        /// Permet d'écrire une erreur dans la log d'évènements
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="message"></param>
        public static void WriteError(string sender, string message)
        {
            try
            {
                EventLog.WriteEntry(Constants.ApplicationName, sender + " : " + message, EventLogEntryType.Error);
            }
            catch (Exception ex)
            {
               throw new Exception("Unable to write on the log event !");
            }
        }

        /// <summary>
        /// Permet d'écrire une information dans la log d'évènements
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="message"></param>
        public static void WriteInfo(string sender, string message)
        {
            try
            {
                EventLog.WriteEntry(Constants.ApplicationName, sender + " : " + message, EventLogEntryType.Information);
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to write on the log event !");
            }
        }

        /// <summary>
        /// Permet de sérialiser une ferme
        /// </summary>
        /// <param name="Obj"></param>
        /// <returns></returns>
        public static String Serialize(Object Obj)
        {
            XmlSerializer serializer = new XmlSerializer(Obj.GetType());
            StringBuilder sb = new StringBuilder();
            StringWriter writer = new StringWriter(sb);
            serializer.Serialize(writer, Obj);
            writer.Close();
            writer.Dispose();
            return sb.ToString();
        }


        /// <summary>
        /// Permet de désérialiser une ferme
        /// </summary>
        /// <param name="str"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static Object Deserialize(String str, Type t)
        {
            XmlSerializer serializer = new XmlSerializer(t);
            StringReader reader = new StringReader(str);

            Object obj = serializer.Deserialize(reader);
            reader.Close();
            reader.Dispose();
            return obj;
        }

        /// <summary>
        /// Permet de récupérer une ressource
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Res(string key)
        {
            ResourceManager resMgr = new ResourceManager("SPFarmModeler", Assembly.GetExecutingAssembly());
            return resMgr.GetString(key);
        }

        /// <summary>
        /// Permet de renvoyer dans une collection de string les valeurs d'un type énuméré
        /// </summary>
        /// <typeparam name="T">Le type énuméré</typeparam>
        /// <returns></returns>
        public static List<string> GetCollectionFromEnum<T>() where T : struct
        {
            List<string> values = new List<string>();
            foreach (object item in Enum.GetValues(typeof(T))) 
            {
                values.Add(item.ToString());
            }
            return values;
        }
    }
}
